﻿/**
 * 默认用户信息(未登录状态)
 */
var DefaultMembership = {
	isLogin : false,
	username: 'noLogin',
	popedom	: {
		linkman	: [
			{allow : false,
			 name  : 'create',
			 text  : '创建新联系人'},
			 
			{allow : false,
			 name  : 'edit',
			 text  : '编辑'},

			{allow : false,
			 name  : 'delete',
			 text  : '删除'}
		],
		
		find : function(className, popedomName){
			var result = false;
			this[className].each(function(popedomPack){
				if (popedomPack['name'] == popedomName) return result = popedomPack['allow'];
			});
			return result;
		}
	}
};

/**
 * 用户控制
 */
var UserCtrler = new Class({
	initialize : function(){
		membership = DefaultMembership;
	},
	/**
	 * 生成用户控制控件并获取用户信息
	 */
	build : function() {
		this.baseElmt = new Element('div', {'id': 'userCtrler'});
		this.infoElmt = new Element('span', {'id': 'userInfo'});
		dataCommunication.login({}, function(result){
			var userData = Json.evaluate(result);
			if (userData.isLogin) {
				membership = $merge(membership, userData);
				this.onLogin();
			}else {
				this.buildLoginForm().injectInside(this.baseElmt);
			}
			this.fireEvent('onComplete');
		}.bind(this), function(){
			this.infoElmt
			.setText('获取用户数据失败,请检查网络连接.')
			.injectInside(this.baseElmt);
		}.bind(this));
		return this.baseElmt;
	},
	
	/**
	 * 生成登录表单
	 */
	buildLoginForm : function() {
		this.loginForm = new Element('form', {'id': 'login'});
		this.loginForm.setHTML(this.loginForm.innerHTML+'用户名:');
		this.userNameBox = new Element('input', {'id': 'username', 'type': 'text'}).injectInside(this.loginForm);
		this.loginForm.setHTML(this.loginForm.innerHTML+'&nbsp;密码:');
		this.passWordBox = new Element('input', {'id': 'password', 'type': 'password'}).injectInside(this.loginForm);
		this.submitButton = new Element('input', {'class': 'button', 'type': 'submit', 'value': '登录'}).injectInside(this.loginForm);
		this.loginForm.addEvent('submit', this.login.bind(this));
		return this.loginForm;
	},
	
	/**
	 * 响应登录事件
	 */
	login : function(e) {
		e = new Event(e).stop().taget;
		var loginData = {};
		loginData.username = $E('#username', e).value.trim();
		loginData.password = $E('#password', e).value;
		for (var n in loginData) if (!loginData[n]) return false;
		this.infoElmt.setText('正在验证数据…').injectInside(this.loginForm);
		dataCommunication.login(loginData, function(result){
			var userData = Json.evaluate(result);
			if (!userData.isLogin) {
				this.infoElmt.setText('用户名或密码错误.');
				return false;
			}
			membership = $merge(membership, userData);
			this.onLogin();
			listCtrler.refreshListTable(listCtrler.openedId);
			this.loginForm.remove();
		}.bind(this), function(){
			this.infoElmt.setText('验证数据提交失败,请检查网络连接.');
		}.bind(this));
	},
	
	/**
	 * 将用户控制控件转换至已登录状态
	 */
	onLogin : function(){
		this.infoElmt.setText(membership.username).injectInside(this.baseElmt);
		new Element('a', {'href': '#', 'id': 'logout'})
		.setText('[注销]')
		.injectInside(this.baseElmt)
		.addEvent('click', function(e){
			e = new Event(e).stop();
			this.logout();
		}.bind(this));
		this.fireEvent('onLogin');
		this.fireEvent('onStateChange');
	},
	
	/**
	 * 响应用户注销事件
	 */
	logout : function() {
		dataCommunication.logout(function(result){
			membership = DefaultMembership;
			this.baseElmt.setHTML('');
			listCtrler.refreshListTable(listCtrler.openedId);
			this.buildLoginForm().injectInside(this.baseElmt);
			this.fireEvent('onLogout');
			this.fireEvent('onStateChange');
		}.bind(this), function(){
			this.infoElmt.setText('提交失败,请检查网络连接.');
		}.bind(this));
	}
})
.implement(new Events);